package com.benefm.multipar.pad.ui;

import android.os.Bundle;
import android.util.DisplayMetrics;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.benefm.multipar.pad.R;
import com.benefm.multipar.pad.databinding.ActivityTestBinding;
import com.benefm.multipar.pad.http.LogInterceptor;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class TestActivity extends AppCompatActivity {

    private ActivityTestBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_test);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
        int screenWidth = displayMetrics.widthPixels;
        int screenHeight = displayMetrics.heightPixels;

        System.out.println("screenWidth = " + screenWidth + "screenHeight=" + screenHeight);


        binding.progressBar.setProgress(50);


        // 直接设置进度（0.0 - 1.0）
        binding.circularProgress.setProgress(0.35f);

// 或者使用动画设置进度
        binding.circularProgress.setProgressWithAnimation(0.8f, 2000); // 1秒内动画到80%进度


        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                try {
                    Thread.sleep(500);
                    int finalI = i;
                    runOnUiThread(() -> {
                        binding.progressBar.setProgress(50 + finalI);
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }).start();
    }


    private void getLocation() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .addInterceptor(new LogInterceptor())
                .build();
        JSONObject object = new JSONObject();
        try {
//            object.put("key","5f09f82f9d28cd16f5bb62dddd405798");
            object.put("key", "62111125878cc5d657836ffaea0b7d5d");
            object.put("location", "116.310003,39.991957");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //设置请求头
        MediaType mediaType = MediaType.parse("application/json");
        //请求体
        okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(mediaType, object.toString());
        Request request = new Request.Builder()
                .post(requestBody)
                .url("https://restapi.amap.com/v3/geocode/regeo")
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                ResponseBody body = response.body();
                if (body != null) {
                    System.out.println(body.string());
                }
            }
        });
    }
}
